#pragma once
#include <libjson.h>
#include "Request.hpp"

using namespace libjson;
template <class T>
class JsonRequest : public Request<T>
{
public:
	JsonRequest() :
		json(JSON_NODE)
	{
	}


public:
	virtual void handler()
	{
		if(false == ((T *)this)->response())
		{
			if( 0 == ((T*)this)->err.str().size() )
			{
				((T*)this)->err << "internal error";
			}

			json.clear();
			json.push_back( JSONNode("errno", -1) );
			json.push_back( JSONNode("error", ((T*)this)->err.str().c_str()) );
		}
		
		printf("Content-type: application/json\r\n"
				"\r\n"
				"%s",
				json.write_formatted().c_str());
	}

public:
	JSONNode	json;	
};

